<template>
  <div class="content">
    <div class="header">
      <h3 class="title">用户列表</h3>
      <el-button type="primary" @click="handleNewUserClick">新建用户</el-button>
    </div>
    <div class="table">
      <el-table :data="usersList" border style="width: 100%">
        <el-table-column align="center" type="selection" width="50" />
        <el-table-column align="center" type="index" label="序号" width="60" />
        <el-table-column
          align="center"
          prop="name"
          label="用户名"
          width="150"
        />
        <el-table-column
          align="center"
          prop="realname"
          label="真实姓名"
          width="150"
        />
        <el-table-column
          align="center"
          prop="cellphone"
          label="手机号码"
          width="150"
        />
        <el-table-column align="center" prop="enable" label="状态" width="100">
          <template #default="scoped">
            <el-tag :type="scoped.row.enable ? 'primary' : 'danger'">
              {{ scoped.row.enable ? '启用' : '禁用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="createAt" label="创建时间">
          <template #default="scoped">
            {{ formatUTC(scoped.row.createAt) }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="updateAt" label="更新时间">
          <template #default="scoped">
            {{ formatUTC(scoped.row.updateAt) }}
          </template>
        </el-table-column>
        <el-table-column align="center" fixed="right" label="操作" width="150">
          <template #default="scoped">
            <el-button
              text
              icon="Edit"
              type="primary"
              size="small"
              @click="handleEditBtnClick(scoped.row)"
            >
              编辑
            </el-button>
            <el-button
              @click="handleDelectBtnClick(scoped.row.id)"
              text
              icon="Delect"
              type="danger"
              size="small"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 50, 100]"
        :total="usersTotalCount"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts" name="content">
import { formatUTC } from '@/utils/format'
import { storeToRefs } from 'pinia'
import userSystemStore from '@/store/main/system/system'
import { ref } from 'vue'
// 自定义事件
const emit = defineEmits(['newClick', 'editClick'])
const currentPage = ref(1)
const pageSize = ref(10)
const systemStore = userSystemStore()
fetchUserListData()
// 展示用户列表数据
const { usersList, usersTotalCount } = storeToRefs(systemStore)

// 分页逻辑
const handleCurrentChange = () => {
  fetchUserListData()
}
const handleSizeChange = () => {
  fetchUserListData()
}
// 触发网络请求
function fetchUserListData(formData = {}) {
  // 请求参数
  const size = pageSize.value
  const offset = (currentPage.value - 1) * size
  const pageInfo = { size, offset }
  // 请求用户列表数据
  const queryInfo = { ...pageInfo, ...formData }
  systemStore.postUsersListAction(queryInfo)
}

// 删除功能
function handleDelectBtnClick(id: number) {
  systemStore.deleteUserByIdAction(id)
}
// 编辑功能
function handleEditBtnClick(row: any) {
  emit('editClick', row)
}

// 格式化UTC时间

// 新建用户
function handleNewUserClick() {
  emit('newClick')
}
defineExpose({
  fetchUserListData
})
</script>

<style lang="less" scoped>
.content {
  background: #fff;
  margin-top: 20px;
  padding: 20px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    margin-bottom: 10px;
    .title {
      font-size: 22px;
    }
  }
}
.table {
  :deep(.el-table__cell) {
    padding: 12px 0;
  }
  .el-button {
    margin-left: 0;
    padding: 5px 8px;
  }
}
.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style>
